package com.ocom.payconfig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfRecordProcess;
import com.ocom.common.entity.card.EntityXfSubAccountList;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.request.callpackage.QuerySubAccountResponse;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfSubAccountList;
import com.ocom.payconfig.service.XfRecordProcessService;
import com.ocom.payconfig.service.XfSubAccountListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Slf4j
@Service
public class XfSubAccountListServiceImpl extends ServiceImpl<IDaoXfSubAccountList, EntityXfSubAccountList> implements XfSubAccountListService {

    @Autowired
    private IDaoXfSubAccountList daoXfSubAccountList;

    @Autowired
    private XfRecordProcessService xfRecordProcessService;


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public QuerySubAccountResponse querySubAccount(Long companyId, Long pId, Integer subAccount) {
        this.getUserSubAccount(companyId, pId, subAccount);
        return daoXfSubAccountList.querySubAccount(companyId, pId, subAccount);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public EntityXfSubAccountList getUserSubAccount(Long companyId, Long pId, Integer subAccount) {
        QueryWrapper<EntityXfSubAccountList> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("p_id",pId);
        queryWrapper.eq("sub_account",subAccount);
        EntityXfSubAccountList entityXfSubAccountList=this.getOne(queryWrapper);
        if(entityXfSubAccountList==null && subAccount>0){
            EntityXfSubAccountList entityXfSubAccountList1 =new EntityXfSubAccountList();
            entityXfSubAccountList1.setComId(companyId);
            entityXfSubAccountList1.setSubAccount(subAccount);
            entityXfSubAccountList1.setPId(pId);
            entityXfSubAccountList1.setAccountRechargeMoney(0);
            entityXfSubAccountList1.setAccountSubsidyMoney(0);
            this.save(entityXfSubAccountList1);
            return  entityXfSubAccountList1;
        }
        return entityXfSubAccountList;
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfSubAccountList> getUserSubAccounts(Long companyId, List<Long> pIds, Integer subAccount) {
        if (pIds == null || pIds.size() == 0) return new ArrayList<>();
        QueryWrapper<EntityXfSubAccountList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("com_id", companyId);
        queryWrapper.in("p_id", pIds);
        queryWrapper.eq("sub_account", subAccount);
        return this.list(queryWrapper);
    }

    @Override
    @Transactional(isolation = Isolation.READ_COMMITTED)
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "companyId")
    public void updateAccountTransactional(Long companyId, Integer id, EntityXfRecordProcess entityXfRecordProcess, EntityXfRecordProcess updateXfRecordProcess) {

        if (entityXfRecordProcess.getChargeMoney() != null || entityXfRecordProcess.getSubMoney() != null){
            //修改金额
            UpdateWrapper<EntityXfSubAccountList> updateWrapper=new UpdateWrapper<>();
            if (entityXfRecordProcess.getChargeMoney() != null && entityXfRecordProcess.getChargeMoney()!=0){
                updateWrapper.setSql("account_recharge_money = account_recharge_money + "+ entityXfRecordProcess.getChargeMoney());
            }
            if (entityXfRecordProcess.getSubMoney() != null && entityXfRecordProcess.getSubMoney()!=0){
                updateWrapper.setSql("account_subsidy_money = account_subsidy_money + "+ entityXfRecordProcess.getSubMoney());
            }
            updateWrapper.eq("com_id",companyId);
            updateWrapper.eq("p_id",entityXfRecordProcess.getPId());
            updateWrapper.eq("sub_account",entityXfRecordProcess.getSubAccount());
            updateWrapper.last("and 0 <= (account_recharge_money + " + entityXfRecordProcess.getChargeMoney() + ") and 0 <= (account_subsidy_money + " + entityXfRecordProcess.getSubMoney() + ")");

            if (!this.update(updateWrapper)){
                log.error("修改金额失败!订单号：{{}}", entityXfRecordProcess.getOrderNo());

                throw new RuntimeException("修改金额失败");
            }
        }

        if (xfRecordProcessService.updateEntityXfRecord(companyId,updateXfRecordProcess)<=0){
            log.error("修改记录失败!订单号：{{}}", entityXfRecordProcess.getOrderNo());
            throw new RuntimeException("修改记录失败");
        }

    }


}
